﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Basic;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Basic
{
    /// <summary>
    /// 获取商品信息请求命令处理类
    /// </summary>
    public class GetGoodsHandler:IRequestHandler<GetGoodsCommand,ApiResult<List<GoodsDTO>>>
    {
        IBaseRepository<Goods_Information> goodsRep;
        IMapper mapper;

        public GetGoodsHandler(IBaseRepository<Goods_Information> goodsRep, IMapper mapper)
        {
            this.goodsRep = goodsRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<GoodsDTO>>> Handle(GetGoodsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<GoodsDTO>> res = new ApiResult<List<GoodsDTO>>();

                var goods = goodsRep.GetAll();

                var model=mapper.Map<List<GoodsDTO>>(goods);

                res.Msg = "获取商品信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
